package com.smp.dsandroidtester;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

import com.smp.dsandroidtester.ds.NativeIntArrayQueue;
import com.smp.dsandroidtester.ds.NativeIntArrayStack;
import com.smp.dsandroidtester.ds.NativeIntLinkedQueue;
import com.smp.dsandroidtester.ds.NativeIntLinkedStack;

public class DSTesterMainActivity extends Activity implements OnClickListener {

	private RadioGroup mrgDS;
	
	//Stack panel
	private LinearLayout mllStackControlPanel;
	private EditText medStackValue;
	private Button mbtnStackPush;
	private Button mbtnStackPop;
	
	//Queue panel
	private LinearLayout mllQueueControlPanel;
	private EditText medQueueValue;
	private Button mbtnQueueOffer;
	private Button mbtnQueuePeek;
	private Button mbtnQueuePoll;
	
	private NativeIntArrayStack mIntArrayStack;
	private NativeIntArrayStack mIntConcurrentArrayStack;
	private NativeIntLinkedStack mIntLinkedStack;
	private NativeIntLinkedStack mIntConcurrentLinkedStack;
	private NativeIntArrayQueue mIntArrayQueue;
	private NativeIntArrayQueue mIntConcurrentArrayQueue;
	private NativeIntLinkedQueue mIntLinkedQueue;
	private NativeIntLinkedQueue mIntConcurrentLinkedQueue;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dstester_main_activity_layout);
		
		mrgDS = (RadioGroup)findViewById(R.id.rgStack);
		mrgDS.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch(checkedId) {
					case R.id.rbArrayStack :
					case R.id.rbConcurrentArrayStack :
					case R.id.rbLinkedStack :
					case R.id.rbConcurrentLinkedStack :
						mllStackControlPanel.setVisibility(View.VISIBLE);
						mllQueueControlPanel.setVisibility(View.GONE);
						break;
					case R.id.rbArrayQueue :
					case R.id.rbConcurrentArrayQueue :
					case R.id.rbLinkedQueue :
					case R.id.rbConcurrentLinkedQueue :
						mllStackControlPanel.setVisibility(View.GONE);
						mllQueueControlPanel.setVisibility(View.VISIBLE);
						break;
				}
			}
		});
		
		mllStackControlPanel = (LinearLayout)findViewById(R.id.llStackControlPanel);
		medStackValue = (EditText)findViewById(R.id.edStackValue);
		mbtnStackPush = (Button)findViewById(R.id.btnStackPush);
		mbtnStackPush.setOnClickListener(this);
		mbtnStackPop = (Button)findViewById(R.id.btnStackPop);
		mbtnStackPop.setOnClickListener(this);
		
		mllQueueControlPanel = (LinearLayout)findViewById(R.id.llQueueControlPanel);
		medQueueValue = (EditText)findViewById(R.id.edQueueValue);
		mbtnQueueOffer = (Button)findViewById(R.id.btnQueueOffer);
		mbtnQueueOffer.setOnClickListener(this);
		mbtnQueuePeek = (Button)findViewById(R.id.btnQueuePeek);
		mbtnQueuePeek.setOnClickListener(this);
		mbtnQueuePoll = (Button)findViewById(R.id.btnQueuePoll);
		mbtnQueuePoll.setOnClickListener(this);
		
		mIntArrayStack = new NativeIntArrayStack(5, false);
		mIntConcurrentArrayStack = new NativeIntArrayStack(5, true);
		mIntLinkedStack = new NativeIntLinkedStack(5, false);
		mIntConcurrentLinkedStack = new NativeIntLinkedStack(5, true);
		
		mIntArrayQueue = new NativeIntArrayQueue(5, false);
		mIntConcurrentArrayQueue = new NativeIntArrayQueue(5, true);
		mIntLinkedQueue = new NativeIntLinkedQueue(5, false);
		mIntConcurrentLinkedQueue = new NativeIntLinkedQueue(5, true);
	}

	@Override
	public void onClick(View view) {
		switch(view.getId()) {
			case R.id.btnStackPush :
				pushToCurrentStack();
				break;
			case R.id.btnStackPop :
				popFromCurrentStack();
				break;
			case R.id.btnQueueOffer :
				offerToCurrentQueue();
				break;
			case R.id.btnQueuePeek :
				peekFromCurrentQueue();
				break;
			case R.id.btnQueuePoll :
				pollFromCurrentQueue();
				break;

		}
	}
	
	private void pushToCurrentStack() {
		String strValue = medStackValue.getText().toString();
		if(TextUtils.isEmpty(strValue)) {
			return;
		}
		
		int value = Integer.parseInt(strValue);
		medStackValue.setText("");
		
		int checkedId = mrgDS.getCheckedRadioButtonId();
		switch(checkedId) {
			case R.id.rbArrayStack :
				if(mIntArrayStack.canPush()) {
					mIntArrayStack.push(value);
					Toast.makeText(DSTesterMainActivity.this, "Value "+value+" is pushed", Toast.LENGTH_SHORT).show();
				}
				else {
					Toast.makeText(DSTesterMainActivity.this, "Not enough stack space", Toast.LENGTH_SHORT).show();
				}
				break;
			case R.id.rbConcurrentArrayStack :
				if(mIntConcurrentArrayStack.canPush()) {
					mIntConcurrentArrayStack.push(value);
					Toast.makeText(DSTesterMainActivity.this, "Value "+value+" is pushed", Toast.LENGTH_SHORT).show();
				}
				else {
					Toast.makeText(DSTesterMainActivity.this, "Not enough stack space", Toast.LENGTH_SHORT).show();
				}
				break;
			case R.id.rbLinkedStack :
				if(mIntLinkedStack.canPush()) {
					mIntLinkedStack.push(value);
					Toast.makeText(DSTesterMainActivity.this, "Value "+value+" is pushed", Toast.LENGTH_SHORT).show();
				}
				else {
					Toast.makeText(DSTesterMainActivity.this, "Not enough stack space", Toast.LENGTH_SHORT).show();
				}
				break;
			case R.id.rbConcurrentLinkedStack :
				if(mIntConcurrentLinkedStack.canPush()) {
					mIntConcurrentLinkedStack.push(value);
					Toast.makeText(DSTesterMainActivity.this, "Value "+value+" is pushed", Toast.LENGTH_SHORT).show();
				}
				else {
					Toast.makeText(DSTesterMainActivity.this, "Not enough stack space", Toast.LENGTH_SHORT).show();
				}
				break;
			default :
				//no op.
				break;
		}
	}
	
	private void popFromCurrentStack() {
		int checkedId = mrgDS.getCheckedRadioButtonId();
		switch(checkedId) {
			case R.id.rbArrayStack :
				if(mIntArrayStack.canPop()) {
					int value = mIntArrayStack.pop();
					Toast.makeText(DSTesterMainActivity.this, "Value "+value+" is popped", Toast.LENGTH_SHORT).show();
				}
				else {
					Toast.makeText(DSTesterMainActivity.this, "No more data in the stack", Toast.LENGTH_SHORT).show();
				}
				break;
			case R.id.rbConcurrentArrayStack :
				if(mIntConcurrentArrayStack.canPop()) {
					int value = mIntConcurrentArrayStack.pop();
					Toast.makeText(DSTesterMainActivity.this, "Value "+value+" is popped", Toast.LENGTH_SHORT).show();
				}
				else {
					Toast.makeText(DSTesterMainActivity.this, "No more data in the stack", Toast.LENGTH_SHORT).show();
				}
				break;
			case R.id.rbLinkedStack :
				if(mIntLinkedStack.canPop()) {
					int value = mIntLinkedStack.pop();
					Toast.makeText(DSTesterMainActivity.this, "Value "+value+" is popped", Toast.LENGTH_SHORT).show();
				}
				else {
					Toast.makeText(DSTesterMainActivity.this, "No more data in the stack", Toast.LENGTH_SHORT).show();
				}
				break;
			case R.id.rbConcurrentLinkedStack :
				if(mIntConcurrentLinkedStack.canPop()) {
					int value = mIntConcurrentLinkedStack.pop();
					Toast.makeText(DSTesterMainActivity.this, "Value "+value+" is popped", Toast.LENGTH_SHORT).show();
				}
				else {
					Toast.makeText(DSTesterMainActivity.this, "No more data in the stack", Toast.LENGTH_SHORT).show();
				}
				break;
			default :
				//no op.
				break;
		}
	}
	
	private void offerToCurrentQueue() {
		String strValue = medQueueValue.getText().toString();
		if(TextUtils.isEmpty(strValue)) {
			return;
		}
		
		int value = Integer.parseInt(strValue);
		medQueueValue.setText("");
		
		int checkedId = mrgDS.getCheckedRadioButtonId();
		switch(checkedId) {
			case R.id.rbArrayQueue :
				if(mIntArrayQueue.canOffer()) {
					mIntArrayQueue.offer(value);
					Toast.makeText(DSTesterMainActivity.this, "Value "+value+" is offered", Toast.LENGTH_SHORT).show();
				}
				else {
					Toast.makeText(DSTesterMainActivity.this, "Not enough queue space", Toast.LENGTH_SHORT).show();
				}
				break;
			case R.id.rbConcurrentArrayQueue :
				if(mIntConcurrentArrayQueue.canOffer()) {
					mIntConcurrentArrayQueue.offer(value);
					Toast.makeText(DSTesterMainActivity.this, "Value "+value+" is offered", Toast.LENGTH_SHORT).show();
				}
				else {
					Toast.makeText(DSTesterMainActivity.this, "Not enough queue space", Toast.LENGTH_SHORT).show();
				}
				break;
			case R.id.rbLinkedQueue :
				if(mIntLinkedQueue.canOffer()) {
					mIntLinkedQueue.offer(value);
					Toast.makeText(DSTesterMainActivity.this, "Value "+value+" is offered", Toast.LENGTH_SHORT).show();
				}
				else {
					Toast.makeText(DSTesterMainActivity.this, "Not enough queue space", Toast.LENGTH_SHORT).show();
				}
				break;
			case R.id.rbConcurrentLinkedQueue :
				if(mIntConcurrentLinkedQueue.canOffer()) {
					mIntConcurrentLinkedQueue.offer(value);
					Toast.makeText(DSTesterMainActivity.this, "Value "+value+" is offered", Toast.LENGTH_SHORT).show();
				}
				else {
					Toast.makeText(DSTesterMainActivity.this, "Not enough queue space", Toast.LENGTH_SHORT).show();
				}
				break;
			default :
				//no op.
				break;
		}
	}
	
	private void peekFromCurrentQueue() {
		
		int checkedId = mrgDS.getCheckedRadioButtonId();
		switch(checkedId) {
			case R.id.rbArrayQueue :
				if(mIntArrayQueue.canPoll()) {
					int value = mIntArrayQueue.peek();
					Toast.makeText(DSTesterMainActivity.this, "Value "+value+" is peeked", Toast.LENGTH_SHORT).show();
				}
				else {
					Toast.makeText(DSTesterMainActivity.this, "No more data in the queue", Toast.LENGTH_SHORT).show();
				}
				break;
			case R.id.rbConcurrentArrayQueue :
				if(mIntConcurrentArrayQueue.canPoll()) {
					int value = mIntConcurrentArrayQueue.peek();
					Toast.makeText(DSTesterMainActivity.this, "Value "+value+" is peeked", Toast.LENGTH_SHORT).show();
				}
				else {
					Toast.makeText(DSTesterMainActivity.this, "No more data in the queue", Toast.LENGTH_SHORT).show();
				}
				break;
			case R.id.rbLinkedQueue :
				if(mIntLinkedQueue.canPoll()) {
					int value = mIntLinkedQueue.peek();
					Toast.makeText(DSTesterMainActivity.this, "Value "+value+" is peeked", Toast.LENGTH_SHORT).show();
				}
				else {
					Toast.makeText(DSTesterMainActivity.this, "No more data in the queue", Toast.LENGTH_SHORT).show();
				}
				break;
			case R.id.rbConcurrentLinkedQueue :
				if(mIntConcurrentLinkedQueue.canPoll()) {
					int value = mIntConcurrentLinkedQueue.peek();
					Toast.makeText(DSTesterMainActivity.this, "Value "+value+" is peeked", Toast.LENGTH_SHORT).show();
				}
				else {
					Toast.makeText(DSTesterMainActivity.this, "No more data in the queue", Toast.LENGTH_SHORT).show();
				}
				break;
			default :
				//no op.
				break;
		}
	}
	
	private void pollFromCurrentQueue() {
		
		int checkedId = mrgDS.getCheckedRadioButtonId();
		switch(checkedId) {
			case R.id.rbArrayQueue :
				if(mIntArrayQueue.canPoll()) {
					int value = mIntArrayQueue.poll();
					Toast.makeText(DSTesterMainActivity.this, "Value "+value+" is polled", Toast.LENGTH_SHORT).show();
				}
				else {
					Toast.makeText(DSTesterMainActivity.this, "No more data in the queue", Toast.LENGTH_SHORT).show();
				}
				break;
			case R.id.rbConcurrentArrayQueue :
				if(mIntConcurrentArrayQueue.canPoll()) {
					int value = mIntConcurrentArrayQueue.poll();
					Toast.makeText(DSTesterMainActivity.this, "Value "+value+" is polled", Toast.LENGTH_SHORT).show();
				}
				else {
					Toast.makeText(DSTesterMainActivity.this, "No more data in the queue", Toast.LENGTH_SHORT).show();
				}
				break;
			case R.id.rbLinkedQueue :
				if(mIntLinkedQueue.canPoll()) {
					int value = mIntLinkedQueue.poll();
					Toast.makeText(DSTesterMainActivity.this, "Value "+value+" is polled", Toast.LENGTH_SHORT).show();
				}
				else {
					Toast.makeText(DSTesterMainActivity.this, "No more data in the queue", Toast.LENGTH_SHORT).show();
				}
				break;
			case R.id.rbConcurrentLinkedQueue :
				if(mIntConcurrentLinkedQueue.canPoll()) {
					int value = mIntConcurrentLinkedQueue.poll();
					Toast.makeText(DSTesterMainActivity.this, "Value "+value+" is polled", Toast.LENGTH_SHORT).show();
				}
				else {
					Toast.makeText(DSTesterMainActivity.this, "No more data in the queue", Toast.LENGTH_SHORT).show();
				}
				break;
			default :
				//no op.
				break;
		}
	}

}
